<template>
  <div class="app-container">
    <el-row :gutter="20">
      <!--分类数据-->
      <el-col :span="5" :xs="24">
        <div class="head-container" style="margin-bottom: 10px" v-if="selectCategory">
          <el-alert :title="selectCategory" type="info" close-text="取消选中" show-icon @close="cancelSelect" />
        </div>
        <div class="head-container">
          <el-input
            v-model="categoryName"
            placeholder="请输入分类名称"
            clearable
            size="small"
            prefix-icon="Search"
            style="margin-bottom: 20px"
          />
        </div>
        <div class="head-container">
          <el-tree
            :data="categoryOptions"
            :props="{ label: 'label', children: 'children' }"
            :expand-on-click-node="false"
            :filter-node-method="filterNode"
            ref="categoryTreeRef"
            node-key="id"
            highlight-current
            default-expand-all
            @node-click="handleNodeClick"
          />
        </div>
      </el-col>
      <!--流程数据-->
      <el-col :span="19" :xs="24">
        <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
          <el-form-item label="名称" prop="name">
            <el-input
              v-model="queryParams.name"
              placeholder="请输入名称"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>

        <el-table v-loading="loading" fit :data="definitionList" border>
          <el-table-column label="流程编号" align="center" prop="deploymentId" :show-overflow-tooltip="true"/>
          <el-table-column label="流程标识" align="center" prop="flowKey" :show-overflow-tooltip="true" />
          <el-table-column label="流程名称" align="center" width="140" :show-overflow-tooltip="true">
            <template slot-scope="scope">
              <el-button type="text" @click="handleReadImage(scope.row.deploymentId)">
                <span>{{ scope.row.name }}</span>
              </el-button>
            </template>
          </el-table-column>
          <el-table-column label="流程版本" align="center" width="80">
            <template slot-scope="scope">
              <el-tag effect="plain" size="small">v{{ scope.row.version }}</el-tag>
            </template>
          </el-table-column>
          <el-table-column label="部署时间" align="center" prop="deploymentTime" width="180"/>
          <el-table-column label="操作" width="150" class-name="small-padding fixed-width">
            <template slot-scope="scope">
              <el-button size="small" icon="el-icon-s-promotion" type="text" @click="handleStartProcess(scope.row)">发起任务</el-button>
            </template>
          </el-table-column>
        </el-table>

        <pagination
          v-show="total>0"
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />
      </el-col>
    </el-row>

    <!-- 流程图 -->
    <el-dialog :title="readImage.title" :visible.sync="readImage.open" width="70%" append-to-body>
      <bpmn-viewer :flowData="flowData"/>
    </el-dialog>

  </div>
</template>

<script>
import {
  applyList,
  delDeployment,
  flowXmlAndNode
} from "@/api/flowable/definition";
import { categoryTree } from "@/api/flowable/category";
import BpmnViewer from '@/components/Process/viewer';

export default {
  name: "Definition",
  dicts: ['sys_process_category'],
  components: {
    BpmnViewer,
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 流程定义表格数据
      definitionList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      formConfOpen: false,
      formTitle: "",
      formDeployOpen: false,
      formDeployTitle: "",
      formList: [],
      formTotal:0,
      formData: {}, // 默认表单数据
      readImage:{
        open: false,
        src: "",
      },
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        category: null,
      },
      formQueryParams:{
        pageNum: 1,
        pageSize: 10,
      },
      deployId: '',
      // xml
      flowData: {},
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      // 流程分类
      selectCategory: null,
      categoryName: null,
      categoryOptions: null,
    };
  },
  created() {
    this.getList();
    this.getCategoryTree();
  },
  watch:{
    categoryName(val){
      this.$refs["categoryTreeRef"].filter(val);
    }
  },
  activated() {
    const time = this.$route.query.t;
    if (time != null) {
      this.getList();
      this.getCategoryTree();
    }
  },
  methods: {
    /** 查询流程定义列表 */
    getList() {
      this.loading = true;
      applyList(this.queryParams).then(response => {
        this.definitionList = response.data.records;
        this.total = response.data.total;
        this.loading = false;
      });
    },
    /** 查询分类下拉树结构 */
    getCategoryTree() {
      categoryTree().then(response => {
        this.categoryOptions = response.data;
      });
    },
    // 筛选节点
    filterNode(value, data) {
      if (!value) return true;
      return data.label.indexOf(value) !== -1;
    },
    // 节点单击事件
    handleNodeClick(data) {
      if (data.parentId === 0) {
        this.$modal.msgWarning("父级节点只做分类展示,请选择子节点进行查询!");
      } else {
        this.queryParams.category = data.id;
        this.selectCategory = data.label;
        this.handleQuery();
      }
    },
    // 取消选中
    cancelSelect() {
      this.selectCategory = null;
      this.queryParams.category = null;
      this.$refs.categoryTreeRef.setCurrentKey(null);
      this.handleQuery();
    },
    /**  发起流程申请 */
    handleStartProcess(row){
      this.$router.push({ path: '/flowable/task/apply/send/index',
        query: {
          deployId: row.deploymentId,
          procDefId: row.id
        }
      })
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.queryParams.category = null;
      this.$refs.categoryTreeRef.setCurrentKey(null);
      this.handleQuery();
    },
    /** 流程图查看 */
    handleReadImage(deployId){
      this.readImage.title = "流程图";
      this.readImage.open = true;
      flowXmlAndNode({deployId:deployId}).then(res => {
        this.flowData = res.data;
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const deploymentIds = row.deploymentId || this.ids;
      this.$confirm('是否确认删除流程定义编号为"' + deploymentIds + '"的数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function() {
        return delDeployment(deploymentIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      })
    },
  }
};
</script>
